﻿@using SmartCode
@using SmartCode.Db
@using SmartCode.Generator
@model BuildContext

@{
    var project = Model.Project;
    var dbSource = Model.GetDataSource<DbSource>();
    var table = Model.GetCurrentTable();

    var resultMapName = $"{table.ConvertedName}ResultMap";

    var buildTask = Model.Build;

    var pkCol = table.PKColumn;
    if (pkCol == null)
    {
        throw new SmartCodeException($"{table.Name} can not find PKColumn！");
    }

    if (!project.BuildTasks.TryGetValue("Entity", out var entityBuild))
    {
        throw new ArgumentException("can not find Project -> BuildTasks -> [Entity] .");
    }

    var entityName = $"{project.Module}.{entityBuild.Module}.{table.ConvertedName}";
    IDbTypeConverter dbTypeConverter = Model.PluginManager.Resolve<IDbTypeConverter>();
}

@functions
{

    String GetJdbcType(IDbTypeConverter dbTypeConverter, DbSource dbSource, String dbType)
    {
        var dbProvider = dbSource.DbRepository.DbProvider;
        var jdbcType = dbTypeConverter.LanguageType(dbProvider, "Jdbc", dbType);
        return String.IsNullOrEmpty(jdbcType) ? "" : $" jdbcType=\"{jdbcType}\"";
    }

}


<resultMap id="@resultMapName" type="@entityName">
    <id column="@pkCol.Name" property="@pkCol.ConvertedName" javaType="@pkCol.LanguageType" @GetJdbcType(dbTypeConverter, dbSource, pkCol.DbType)></id>
    @foreach (var col in table.Columns)
    {
        if (col.IsPrimaryKey)
        {
            continue;
        }

        <result column="@col.Name" property="@col.ConvertedName" javaType="@col.LanguageType" @GetJdbcType(dbTypeConverter, dbSource, col.DbType)/>
    }
</resultMap>